package com.invengo.module_check.ui.fragment.first_check;

import static com.invengo.module_check.data.entity.ActionArgs.ACTION_FIRST_CHECK;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;

import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.BundleKey;
import com.invengo.module_check.databinding.FragmentFirstSelectShelfBinding;
import com.invengo.module_check.ui.fragment.widget.SelectShelfDialog;

import java.io.Serializable;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.KLog;

public class FirstSelectShelfFragment extends BaseFragment<FragmentFirstSelectShelfBinding, FirstSelectShelfViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_first_select_shelf;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        changeForce(true);
    }

    private void changeForce(boolean isFirst){
        new Handler().postDelayed(() -> {
            KLog.e("2秒后执行切换操作"+isFirst);
            if (isFirst)
                binding.first.requestFocus();
            else
                binding.second.requestFocus();
        },400);
    }

    @Override
    public void initViewObservable() {
        viewModel.ui.multiChangeEvent.observe(this, b -> changeForce(b));
        viewModel.ui.nextEvent.observe(this, shelfBeans -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.KEY_SHELFS, (Serializable) shelfBeans);
            bundle.putSerializable(BundleKey.KEY_ACTION, ACTION_FIRST_CHECK);
            bundle.putSerializable(BundleKey.KEY_START_SHELF_CODE, viewModel.firstShelfCode.get());
            bundle.putSerializable(BundleKey.KEY_END_SHELF_CODE, viewModel.secondShelfCode.get());
            NavHostFragment.findNavController(this).navigate(ACTION_FIRST_CHECK.getFirst2second(), bundle);
        });
        viewModel.ui.selectShelfEvent.observe(this, bean -> SelectShelfDialog.getInstance().showDialog(getActivity(), bean, bean1 -> {
            viewModel.handlerShelf(bean1);
        }));
    }
}
